package cn.edu.usst.cs.goldfish.booking.meetingroom.service.impl;

import cn.edu.usst.cs.goldfish.booking.meetingroom.handler.GoldfishBookingException;
import cn.edu.usst.cs.goldfish.booking.meetingroom.mapstruct.AuthMapper;
import cn.edu.usst.cs.goldfish.booking.meetingroom.model.SysUser;
import cn.edu.usst.cs.goldfish.booking.meetingroom.mybatis.UserMapper;
import cn.edu.usst.cs.goldfish.booking.meetingroom.service.UserService;
import cn.edu.usst.cs.goldfish.booking.meetingroom.util.EmailUtil;
import cn.edu.usst.cs.goldfish.booking.meetingroom.web.dto.auth.UserPreview;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    private EmailUtil emailUtil;
    private AuthMapper authMapper;
    private SpringTemplateEngine templateEngine;

    public UserServiceImpl(
            UserMapper userMapper,
            EmailUtil emailUtil,
            AuthMapper authMapper,
            SpringTemplateEngine templateEngine) {
        this.userMapper = userMapper;
        this.emailUtil = emailUtil;
        this.authMapper = authMapper;
        this.templateEngine = templateEngine;
    }

    @Override
    public SysUser findByCredential(String identity) {
        SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().eq("username", identity).or().eq("email", identity));
        return user;
    }

    @Override
    public long sendCaptcha(UserPreview userPreview) {
        long code = generateCode();
        Context context = new Context();
        context.setVariable("code", code);
        context.setVariable("name", userPreview.getUsername());
        EmailUtil.EmailInfo.EmailInfoBuilder emailInfoBuilder = EmailUtil.EmailInfo.builder();
        emailInfoBuilder.email(userPreview.getEmail());
        emailInfoBuilder.content(templateEngine.process("captcha-email", context));
        emailUtil.sendEmail(emailInfoBuilder.build());
        return code;
    }


    @Override
    public void createUserFromPreview(UserPreview preview) {
        SysUser user = authMapper.registerRequestToUser(preview);
        userMapper.insert(user);
    }

    @Override
    public void ensureEmailNotTaken(String email) {
        SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().eq("email", email));
        if (user != null) {
            throw new GoldfishBookingException("邮箱已被" + user.getUsername() + "使用");
        }
    }

    @Override
    public void ensureNicknameNotTaken(String nickname) {
        SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().eq("username", nickname));
        if (user != null) {
            throw new GoldfishBookingException("昵称已被" + user.getEmail() + "使用");
        }
    }

    @Override
    public String findEmailByIdentifier(String identifier) {
        SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().eq("username", identifier).or().eq("email", identifier));
        return (user != null) ? user.getEmail() : null;
    }

    private long generateCode() {
        return (new Random().nextInt(899999) + 100000);
    }
}
